import * as pdfjs from 'pdfjs-dist'
import * as pdfjsWorker from 'pdfjs-dist/build/pdf.worker.entry'
pdfjs.GlobalWorkerOptions.workerSrc = pdfjsWorker
let pageurl
async function printPDF (pdfData) {
  pageurl = pdfData
  // Using DocumentInitParameters object to load binary data.
  let loadingTask = pdfjs.getDocument({
    url: pdfData
  })
  return loadingTask.promise
    .then((pdf) => {
      // const numPages = pdf.numPages
      return pdf.getPage(1)
        .then((page) => {
          return pdfTocanvas(page)
        })
    })
}
async function pdfTocanvas (page) {
  let viewport = page.getViewport({
    scale: 1
  })
  // console.log(page._pageInfo.view[3], page._pageInfo.view[2])
  const canvas = document.createElement('canvas')
  const context = canvas.getContext('2d')
  canvas.height = viewport.height
  canvas.width = viewport.width
  const renderContext = {
    canvasContext: context,
    viewport: viewport,
    intent: 'speed'
  }
  viewport = null
  const renderTask = page.render(renderContext)
  return renderTask.promise.then(() => canvas)
}

export {printPDF}
